package org.tbjd.client.states;

import java.util.Date;

import org.tbjd.client.beans.Task;
import org.tbjd.client.exceptions.DateManagerBeginDateException;
import org.tbjd.client.exceptions.DateManagerDurationException;
import org.tbjd.client.exceptions.DateManagerEffortException;
import org.tbjd.client.exceptions.DateManagerEndDateException;
import org.tbjd.client.exceptions.DateManagerProgressException;

public class StateEndFrozenASAP extends AbstractTaskState {

	public StateEndFrozenASAP() {
		setASAPChainingMode(true);
	}

	public StateEndFrozenASAP(AbstractTaskDurationState durationState) {
		this();
		setDurationState(durationState);
	}

	public void beginDateChanged(Task task, Date newBeginDate) throws DateManagerBeginDateException {
		task.setBeginDate(newBeginDate);
		task.setDuration(getDurationModeState().getDuration(task));
		task.setState(new StateEndFrozenSPEC(task.getState().getDurationModeState()));
		task.getState().setDurationState(new StateDurationByDates());

	}

	public void durationChanged(Task task, float newDuration) throws DateManagerDurationException {
		task.setBeginDate(getBeginDate(task.getEndDate(), newDuration));
		task.setDuration(newDuration);
		task.getState().setDurationState(new StateDurationByDates());
		task.setDurationModeByRatios(false);
		task.setState(new StateEndFrozenSPEC(task.getState().getDurationModeState()));
	}

	public void effortChanged(Task task, float newEffort) throws DateManagerEffortException {
		task.setEffort(newEffort);
		task.setDuration(getDurationModeState().getDuration(task));
		task.setBeginDate(getBeginDate(task.getEndDate(), task.getDuration()));
		task.setState(new StateEndFrozenSPEC(task.getState().getDurationModeState()));
	}

	public void endDateChanged(Task task, Date newEndDate) throws DateManagerEndDateException {
		task.setBeginDate(getBeginDate(task, newEndDate));
		task.setEndDate(newEndDate);
		task.setState(new StateEndFrozenSPEC(task.getState().getDurationModeState()));
	}

	public void resourceChanged(Task task) {
		task.setDuration(getDurationModeState().getDuration(task));
		task.setBeginDate(getBeginDate(task.getEndDate(), task.getDuration()));
		task.setState(new StateEndFrozenSPEC(task.getState().getDurationModeState()));
	}

	@Override
	public void progressChanged(Task task, int newProgress) throws DateManagerProgressException {
		task.setProgress(newProgress);		
	}

}
